package net.lookyou.boot.demo.config.security;

import net.lookyou.boot.demo.common.ApiResult;
import net.lookyou.boot.demo.utils.ResponseUtils;
import net.lookyou.boot.demo.entity.SysUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  <p> 认证成功处理 </p>
 *
 * @description :
 * @author : zhengqing
 * @date : 2019/10/12 15:31
 */
@Component
public class CustomerAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication auth) throws IOException, ServletException {
        SysUser user = new SysUser();
        User securityUser = ((User) auth.getPrincipal());
        //user.setToken(securityUser.getToken());
        user.setUsername(securityUser.getUsername());
        ResponseUtils.out(response, ApiResult.ok("登录成功!", user));
    }

}
